<?php
class EditionsController extends AppController {

	var $name = 'Editions';
	var $uses = array('Problem','Practice');
	
    function beforeFilter () {
        parent::beforeFilter();
    }
	
	function index() {
        $this->set('title_nav_bar','Modificaciones');
//		$this->set('planes', $this->paginate());
	}


	//---------------------------------------------------------------------------------- PROBLEMAS

	function list_problem() {
		if ( isset( $_POST['filtro'] ) ) { 
			$filtro=$_POST['filtro']; 
		} else { 
			if (isset($_SESSION['filtro']) ) {
				$filtro = $_SESSION['filtro'];	
			} else {
				$filtro='';	
			}
			
		}
		$_SESSION['filtro'] = $filtro;
        $this->set('title_nav_bar','Listado de Problemas');
		$this->set('filtro',$filtro);
		$this->set('problems', $this->paginate('Problem', array('Problem.name LIKE' => "%$filtro%")) );
	}


    function edit_problem($id=0) {

		if ( isset($_POST['save']) ) {
			
			if ($this->Problem->save($this->data['Problem'])) {
				$this->Session->setFlash(__('Guardado correctamente', true));
				$this -> redirect(array('action'=>'list_problem'));
				exit();                
			} else {
				$this->Session->setFlash(__('No se pudo guardar el formulario.', true));
			}  
			
		} 
            
        if ($id == 0) {
                $this->set('title_nav_bar','Crear un Problema');
                // Nuevo Plan
                $T = $this->Problem->_schema;
                foreach($T as $clave => $valor) { $this->data['Problem'][$clave] = $valor['default']; }
                $this->data['Problem']['id'] = 0;    
        } else {
                $this->set('title_nav_bar','Editar Problema (id:'.$id.')');
				$this->Problem->recursive = 3;
                $this->data= $this->Problem->read(null, $id);
			
        }
		
    }


	function delete_problem() {

		if (empty($this->data['Delete'])) {
			$this->Session->setFlash(__('Debe seleccionar un plan', true));
			$this->redirect(array('action'=>'index'));
		} else {
 
            foreach($this->data['Delete'] as $key=>$value) {
                if ($value==1) { 
                    $this->Problem->delete($key,true); 
                }
            }
            $this->Session->setFlash(__('Problema/s eliminado/s', true));
    		$this->redirect(array('action'=>'list_problem'));

		} 
	}

	function limpiar_filtro() {
		$_SESSION['filtro'] = '';
		$this -> redirect(array('action'=>'list_problem'));
		exit();                
	}


	//---------------------------------------------------------------------------------- PRACTICAS

	function list_practice() {
		if ( isset( $_POST['filtro'] ) ) { 
			$filtro=$_POST['filtro']; 
		} else { 
			if (isset($_SESSION['filtro']) ) {
				$filtro = $_SESSION['filtro'];	
			} else {
				$filtro='';	
			}
			
		}
		$_SESSION['filtro'] = $filtro;
        $this->set('title_nav_bar','Listado de Practicas');
		$this->set('filtro',$filtro);
		$this->set('practices', $this->paginate('Practice', array('Practice.name LIKE' => "%$filtro%")) );
	}


    function edit_practice($id=0) {

		if ( isset($_POST['save']) ) {
			
			if ($this->Practice->save($this->data['Practice'])) {
				$this->Session->setFlash(__('Guardado correctamente', true));
				$this -> redirect(array('action'=>'list_practice'));
				exit();                
			} else {
				$this->Session->setFlash(__('No se pudo guardar el formulario.', true));
			}  
			
		} 
            
        if ($id == 0) {
                $this->set('title_nav_bar','Crear una Practica');
                // Nuevo Plan
                $T = $this->Practice->_schema;
                foreach($T as $clave => $valor) { $this->data['Practice'][$clave] = $valor['default']; }
                $this->data['Practice']['id'] = 0;    
        } else {
                $this->set('title_nav_bar','Editar Practica (id:'.$id.')');
				$this->Practice->recursive = 3;
                $this->data= $this->Practice->read(null, $id);
			
        }
		
    }


	function delete_practice() {

		if (empty($this->data['Delete'])) {
			$this->Session->setFlash(__('Debe seleccionar un plan', true));
			$this->redirect(array('action'=>'index'));
		} else {
 
            foreach($this->data['Delete'] as $key=>$value) {
                if ($value==1) { 
                    $this->Practice->delete($key,true); 
                }
            }
            $this->Session->setFlash(__('Practica/s eliminada/s', true));
    		$this->redirect(array('action'=>'list_practice'));

		} 
	}

	function limpiar_filtro_practice() {
		$_SESSION['filtro'] = '';
		$this -> redirect(array('action'=>'list_practice'));
		exit();                
	}

	
	
}
